package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository cr;

	// 查找未关联
	@Override
	public List<Customer> findNoAssCustomer() {
		List<Customer> list = cr.findByFixedAreaIdIsNull();
		return list;
	}

	// 查找已关联
	@Override
	public List<Customer> findAssCustomer(String FixedAreaId) {
		List<Customer> list = cr.findByFixedAreaId(FixedAreaId);
		return list;
	}

	// 关联操作
	@Override
	public void doAssCustom(String str, String FixedAreaId) {
		// 先让所有的客户都把FixedAreaId 置为0
		cr.deleteFixedAreaId(FixedAreaId);
		// 判断非空
		if ("null".equals(str)) {
			System.out.println(str);
			// System.out.println(str==true);
			// cr.deleteFixedAreaId(FixedAreaId);
		} else {
			// cr.deleteFixedAreaId(FixedAreaId);
			String[] sts = str.split(",");
			// 遍历操作
			for (String s : sts) {
				int id = Integer.parseInt(s);
				System.out.println("id------   ------" + id);
				cr.updateFixedAreaId(FixedAreaId, id);

			}
		}
	}

	@Override
	public Customer findCustomer(String teltphone, String password) {
		return cr.findByTelephoneAndPassword(teltphone, password);
		// return null;
	}

	@Override
	public String findFixedIdByAddress(String address) {
		return cr.findFixedIdByAddress(address);
		//return c.getFixedAreaId();
	}

}
